#ifndef __RADAR_H
#define __RADAR_H

#include "protocal_define.h"
#include "respond_struct_define.h"
#include <condition_variable>
#include <fstream>
#include <functional>
#include <list>
#include <mutex>

#define IQ_GROUP_COUNT 262144

class Radar {
  public:
    Radar(char *signalefilepath);
    ~Radar();

  public:
    std::list<signaltable_t> *returnSignalFound();
    std::list<signaltable_t> *returnSignalUnderDetection();
    std::mutex *returnMutex();

    void set_sense_config_parameters(sense_config_para_t *parameters);

    void emit_signal(target_signal_def *parameters);
    void clearSignalUnderDetection();
    void addSignalUnderDetection(uint16_t id, uint32_t frequency, uint16_t bandwidth, int16_t attenuation);
    void senseTask();
    void narrowBandScanTask();

    void timeSyncTask();
    void notifyTimeSync();

    void notifySense();
    void notifyNarrowBandScan();
    void stopNarrowBandScan();
    void startIQ_TX_Task();
    void stopIQ_TX_Task();
    void setSendIQCallback(const std::function<void(signaltable_t *signaltable)> func) {
        send_IQ_func = func;
    }
    void setSendSignalTableCallback(const std::function<void()> func) {
        send_signal_table_func = func;
    }

  private:
    std::list<signaltable_t> m_signal_found;
    std::list<signaltable_t> m_signal_under_detection;
    std::mutex m_mutex;
    std::condition_variable m_sense_cond;
    std::condition_variable m_narrow_band_scan_cond;

    std::mutex m_time_sync_mutex;
    std::condition_variable m_time_sync_cond;
    bool trigger_time_sync = false;

    sense_config_para_t m_payload_parameters;

    bool task_IQ_TX_is_running = 1;
    bool IQ_store_enable = 0;
    bool sense_ready = false;
    bool narrow_band_scan_ready = false;
    std::ofstream fout;
    char *signal_file_path;
    std::function<void(signaltable_t *signaltable)> send_IQ_func;
    std::function<void()> send_signal_table_func;
};

#endif
